package com.itita.ww2.puredb.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.google.gson.GsonBuilder;
import com.itita.ww2.game.worldmap.WorldMapManager;
import com.itita.ww2.model.worldmap.City;
import com.itita.ww2.puredb.DAO.UserCityConstructionMapper;
import com.itita.ww2.utils.MyBatisSessionUtil;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class UserCityConstruction {

	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.uuid
	 * @mbg.generated
	 */
	private String uuid;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.uid
	 * @mbg.generated
	 */
	private int uid;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.code
	 * @mbg.generated
	 */
	private String code;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.block_id
	 * @mbg.generated
	 */
	private int blockId;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.city_id
	 * @mbg.generated
	 */
	private int cityId;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.scheme
	 * @mbg.generated
	 */
	private int scheme;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.lv
	 * @mbg.generated
	 */
	private int lv;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_city_construction.buyTime
	 * @mbg.generated
	 */
	private long buyTime;

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.uuid
	 * @return  the value of user_city_construction.uuid
	 * @mbg.generated
	 */
	public String getUuid() {
		return uuid;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.uuid
	 * @param uuid  the value for user_city_construction.uuid
	 * @mbg.generated
	 */
	public void setUuid(String uuid) {
		this.uuid = uuid == null ? null : uuid.trim();
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.uid
	 * @return  the value of user_city_construction.uid
	 * @mbg.generated
	 */
	public int getUid() {
		return uid;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.uid
	 * @param uid  the value for user_city_construction.uid
	 * @mbg.generated
	 */
	public void setUid(int uid) {
		this.uid = uid;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.code
	 * @return  the value of user_city_construction.code
	 * @mbg.generated
	 */
	public String getCode() {
		return code;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.code
	 * @param code  the value for user_city_construction.code
	 * @mbg.generated
	 */
	public void setCode(String code) {
		this.code = code == null ? null : code.trim();
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.block_id
	 * @return  the value of user_city_construction.block_id
	 * @mbg.generated
	 */
	public int getBlockId() {
		return blockId;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.block_id
	 * @param blockId  the value for user_city_construction.block_id
	 * @mbg.generated
	 */
	public void setBlockId(int blockId) {
		this.blockId = blockId;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.city_id
	 * @return  the value of user_city_construction.city_id
	 * @mbg.generated
	 */
	public int getCityId() {
		return cityId;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.city_id
	 * @param cityId  the value for user_city_construction.city_id
	 * @mbg.generated
	 */
	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.scheme
	 * @return  the value of user_city_construction.scheme
	 * @mbg.generated
	 */
	public int getScheme() {
		return scheme;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.scheme
	 * @param scheme  the value for user_city_construction.scheme
	 * @mbg.generated
	 */
	public void setScheme(int scheme) {
		this.scheme = scheme;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.lv
	 * @return  the value of user_city_construction.lv
	 * @mbg.generated
	 */
	public int getLv() {
		return lv;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.lv
	 * @param lv  the value for user_city_construction.lv
	 * @mbg.generated
	 */
	public void setLv(int lv) {
		this.lv = lv;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_city_construction.buyTime
	 * @return  the value of user_city_construction.buyTime
	 * @mbg.generated
	 */
	public long getBuyTime() {
		return buyTime;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_city_construction.buyTime
	 * @param buyTime  the value for user_city_construction.buyTime
	 * @mbg.generated
	 */
	public void setBuyTime(long buyTime) {
		this.buyTime = buyTime;
	}

	private transient City city;

	public void setCity(City city) {
		this.city = city;
		this.blockId = city.getBlockId();
		this.cityId = city.getId();
	}

	public City getCity() {
		if (city == null) {
			setCity(WorldMapManager.getInstance().getCity(blockId, cityId));
		}
		return city;
	}

	public ISFSObject toSFSObject() {
		ISFSObject ref = SFSObject.newFromJsonData(new GsonBuilder().create().toJson(this));
		ref.removeElement("uid");
		ref.removeElement("buyTime");
		return ref;
	}

	public void insert() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserCityConstructionMapper.class).insert(this);
			session.commit();
		} finally {
			session.close();
		}
	}

	public void update() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserCityConstructionMapper.class).updateByPrimaryKey(this);
			session.commit();
		} finally {
			session.close();
		}
	}

	public void delete() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserCityConstructionMapper.class).deleteByPrimaryKey(this.uuid);
			session.commit();
		} finally {
			session.close();
		}
	}

	/**
	 * 获取玩家拥有的某种建设总量
	 */
	public static int getUserConstructionCount(String code, int uid) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		int ref = 0;
		try {
			ref = session.getMapper(UserCityConstructionMapper.class).selectCountByCode(code, uid);
		} finally {
			session.close();
		}
		return ref;
	}

	/**
	 * 通过uuid获取城池建设
	 */
	public static UserCityConstruction getByUUID(String uuid) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		UserCityConstruction ref = null;
		try {
			ref = session.getMapper(UserCityConstructionMapper.class).selectByPrimaryKey(uuid);
		} finally {
			session.close();
		}
		return ref;
	}

	/**
	 * 查询对应建设是否存在
	 */
	public static boolean cityConstructionExists(int uid, String code, int blockId, int cityId) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		boolean ref = false;
		try {
			ref = session.getMapper(UserCityConstructionMapper.class).selectCountByCityConstruction(uid, code, blockId,
					cityId) > 0;
		} finally {
			session.close();
		}
		return ref;
	}

	public static boolean anyConstructionExists(int uid) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		boolean ref = false;
		try {
			ref = session.getMapper(UserCityConstructionMapper.class).selectCountByUid(uid) > 0;
		} finally {
			session.close();
		}
		return ref;
	}

	public static List<UserCityConstruction> getByUid(int uid) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		List<UserCityConstruction> ref = new ArrayList<>();
		try {
			ref = session.getMapper(UserCityConstructionMapper.class).selectAllByUid(uid);
		} finally {
			session.close();
		}
		return ref;
	}

	public static void updateSchemeBatch(List<UserCityConstruction> data) {
		if (data.size() <= 0) {
			return;
		}
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserCityConstructionMapper.class).updateSchemeBatch(data);
			session.commit();
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
	}
}